<?php
require_once 'bootstrap.php';
include_once 'includes/formvalidator.php';
include_once 'includes/utils.php';

if (isset($_POST['Submit'])) {
	foreach ($_POST as $key => $value) {
		if ($key !== "Submit") {
			$$key = addslashes(trim($value));
		}
	}

	// Validation
	$validations = array(
		'url' => array('type' => 'url', 'message' => 'You must enter a valid url'),
		'name' => array('type' => 'alfanum', 'message' => 'You must enter a valid name (Alpha Numeric)'),
		'desc' => array('type' => 'anything', 'message' => 'You must enter a valid description'),
		'zipcode' => array('type' => 'anything', 'message' => 'You must enter a valid Zipcode (Numeric)'),
		'email' => array('type' => 'email', 'message' => 'You must enter a valid email address'),
	);
	// Required array
	$required = array('url', 'name', 'desc', 'zipcode', 'email' );

	$sanatize = array();
	foreach ($validations as $item_name => $options) {
		switch ($options['type']) {
			case 'anything':
				array_push($sanatize, $item_name);
				break;
			case 'number':
				$sanatize[$item_name] = 'int';
				break;
			case 'price':
				$sanatize[$item_name] = 'float';
				break;
			case 'email':
				$sanatize[$item_name] = 'email';
				break;
			default:
				break;
		}
	}
	$validator = new FormValidator($validations, $required, $sanatize);

	if ($validator->validate($_POST)) {
		// Very important!!
		$_POST = $validator->sanatize($_POST);

		$data = array($_POST['url'], $_POST['name'], $_POST['desc'], $_POST['zipcode'], $_POST['email']);
		$my_db->execute('INSERT ads (`link`,`name`,`descr`,`zipcode`,`email`) VALUES (?, ?, ?, ?, ?)', $data);
		$mess = getADMSuccess("Ads have been updated.");
		$_POST = array();
	}else {
		$form_errors = $validator->getError();

		if (!empty($form_errors)) {
			foreach ($form_errors as $error) {
				$errors .= li($error);
			}
			$mess = getADMError($errors);
		}
	}
}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?php echo $front_title; ?></title>
<?php include("__head_tag.php");?>
</head>
<body>
<div id="wrap">
<?php include("__header.php"); ?>

<?php include("__nav.php"); ?>

<div id="content">
  <p>&nbsp;</p>
<h2 class="header">Post your ad </h2>
	<?php echo $mess;?>
<p>
	<form action="" name="f1" id="f1" method="post" class="theForm" onsubmit="return validate();">
	  <table width="100%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td width="38%" class="theField">Link URL :</td>
          <td width="62%"><input name="url" value="<?php echo _getEcho($_POST,'url'); ?>" type="text" class="theInput require" id="url" size="30" maxlength="255" /></td>
        </tr>
        <tr>
          <td class="theField"> Link Name :</td>
          <td>
          <input name="name" value="<?php echo _getEcho($_POST,'name'); ?>" type="text" class="theInput require" id="name" size="30" maxlength="30" /></td>
        </tr>
        <tr>
          <td class="theField">Little Description :</td>
          <td><input name="desc" value="<?php echo _getEcho($_POST,'desc');?>" type="text" class="theInput require" id="desc" size="30" maxlength="255" /></td>
        </tr>
        <tr>
          <td class="theField"> Zipcode for Ad :</td>
          <td><input name="zipcode" value="<?php echo _getEcho($_POST,'zipcode'); ?>" type="text" class="theInput require" id="zipcode" size="30" maxlength="10" onkeypress="return isNumberKey(event)" /></td>
        </tr>
        <tr>
          <td class="theField">Your email address :</td>
          <td><input name="email" value="<?php echo _getEcho($_POST,'email'); ?>" type="text" class="theInput require" id="email" size="30" maxlength="150" /></td>
        </tr>
		  <tr>
			  <td>
				  <div align="right">
					  <a href="#" onclick="
            document.getElementById('captcha').src='captcha.php?'+Math.random();
            document.getElementById('captcha-form').focus();"
					     id="change-image">Not readable? Change text.</a>
				  </div>
			  </td>
			  <td><img src="captcha.php" id="captcha"/></td>
		  </tr>
		  <tr>
			  <td class="theField">Image Verification :</td>
			  <td><input name="captcha" type="text" class="theInput require" id="captcha" size="30" maxlength="100"/></td>
		  </tr>
		  <tr>
			  <td colspan="2">
				  <hr/>
			  </td>
		  </tr>
        <tr>
          <td colspan="2"><div align="center">
            <input name="Submit" type="submit" class="theButton" id="Submit" value="Submit" />
          </div></td>
        </tr>
      </table>
    
    </form>
</p>
</div>
<?php include("__footer.php"); ?>
</div>
</body>
</html>
